//地区三级联动,需jqery.select.js支持
(function ($) {

	$.fn.extend({
		/**
		 * 说明:构造函数
		 * settings:参数
		 */
		selectArea:function (settings) {
			var mythis=this;
			var setvar = {//外部设置变量
				'province':'',//省input *必需
				'area':'',//地级市input *必需
				'city':'',//县市区input *必需
				'provinceNull':false,
				'areaNull':false,
				'cityNull':false,
				callback:function(val,text){},
				'selected':""
			};//设置变量
			$.extend(setvar, settings);
			//json=[]
			var vardefaults=null;
			if(setvar.provinceNull==true){
				vardefaults=0;
			}

			var oInput=setvar.city.attr('id');//表单的id
			if(mythis.next("#"+oInput+"_select").length>0){
				selectObjects=mythis.next("#"+oInput+"_select");
			}else if(mythis.parents("form").length>0){
				oForm=mythis.parents("form").attr('id');
				selectObjects=$("#"+oForm).find("#"+oInput+"_select");
			}else{
				selectObjects=$("#"+oInput+"_select");
			}//select按扭对像，有form时加上form为前缀

			//选择值生成
			var provinceSelected='',areaSelected='',citySelected='';
			if(setvar.selected!=''){
				provinceSelected=setvar.selected.substring(0,2)+'0000';
				areaSelected=setvar.selected.substring(0,4)+'00';
				citySelected=setvar.selected;
			}

			//alert(setvar.selected.substring(4,6));

			var oprovince=setvar.province.select({
				//optionData:json,
				boxWidth: 660,
				boxHeight: 400,//下拉层的高
				optionli:'',
				defaults:null,
				defaultFill:[{"value": '', "text": "省/直辖市"}],
				css:'selectareaOptions',
				callback:function(val,text) {
					mythis.val(val);
					if (val != 0) {
						if(val.substring(4,6)!='00'){
							var _provinceSelected='',_areaSelected='',_citySelected='';
							if(val!=''){
								_provinceSelected=val.substring(0,2)+'0000';
								_areaSelected=val.substring(0,4)+'00';
								_citySelected=val;
							}
							quickSelection(this.optionData,_provinceSelected,_areaSelected,_citySelected);//mythis,setvar,this.optionData,_provinceSelected,_areaSelected,_citySelected
						}else{
							$.ajax({
								type: "GET",
								url: "/common/area/" + val,
								dataType: "json",
								success: function (data) {
									if(setvar.areaNull==true){
										json=[{"value":'',"text":text+"-不限"}]
										json=json.concat(data);
									}else{
										json=data;
									}
									if(data==''){
										json=[{"value":val,"text":text,"selected":true}]
										oarea.resetOptions(json);
										ocity.resetOptions(json);
										setvar.callback(val,text);
									}else{
										oarea.resetOptions(json);
										oarea.optionsEvents();
										ocity.resetOptions([]);
									}
								}
							});
						}
					}else{
						oarea.resetOptions([]);
						ocity.resetOptions([]);
					}
				}
			});

			$.ajax({
				type:"GET",
				url:"/common/shortcut",
				dataType:"json",
				success: function(data){
					if(setvar.provinceNull==true){
						json=[{"value":'f1',"text":"快选","section":true}]
						json=json.concat(data.city);
						json=json.concat([{"value":'f2',"text":"省/直辖市","section":true}]);
						json=json.concat([{"value":'',"text":"省-不限"}]);
						json=json.concat(data.province);
					}else{
						json=[{"value":'f1',"text":"快选","section":true}]
						json=json.concat(data.city);
						json=json.concat([{"value":'f2',"text":"省/直辖市","section":true}]);
						json=json.concat(data.province);
					}
					oprovince.resetOptions(json);
					quickSelection(data.province,provinceSelected,areaSelected,citySelected);//快选函数//mythis,setvar,data.province,provinceSelected,areaSelected,citySelected
				}
			});

			//json=[{"value":0,"text":"地级市:"}]
			var vardefaults=null;
			if(setvar.areaNull==true){
				vardefaults=0;
			}
			var oarea=setvar.area.select({
				//optionData:json,
				css:'selectareaOptions',
				boxWidth: 440,
				optionli:'',
				defaults:vardefaults,
				defaultFill:[{"value": '', "text": "地级市"}],
				callback:function(val,text) {
					if (val != 0) {
						mythis.val(val);
						$.ajax({
							type: "GET",
							url: "/common/city/" + val,
							dataType: "json",
							success: function (data) {
								if(setvar.cityNull==true){
									json=[{"value":'',"text":text+"-不限"}]
									json=json.concat(data);
								}else{
									json=data;
								}
								if(data==''){
									json=[{"value":val,"text":text,"selected":true}]
									ocity.resetOptions(json);
									setvar.callback(val,text);
								}else {
									ocity.resetOptions(json);
									ocity.optionsEvents();
								}
							}
						});
					}else{
						ocity.resetOptions([]);
						mythis.val(setvar.province.val());
					}
				}
			});

			//json=[{"value":0,"text":"县市区:"}]
			var vardefaults=null;
			if(setvar.cityNull==true){
				vardefaults=0;
			}
			var ocity=setvar.city.select({
				//optionData:json,
				css:'selectareaOptions',
				boxWidth: 440,
				optionli:'',
				defaults:vardefaults,
				defaultFill:[{"value": '', "text": "县/市/区"}],
				callback:function(val,text) {
					if(val!=0){
						mythis.val(val);
						setvar.callback(val,text);//回调
					}else{
						mythis.val(setvar.area.val());
						setvar.callback(setvar.area.val(),text);//回调
					}
				}
			});
			function quickSelection(data,provinceSelected,areaSelected,citySelected){//mythis,setvar,data,provinceSelected,areaSelected,citySelected//将函数移动内部，这些参数就不用传了
				if(provinceSelected!=''){
					oprovince.resetSelected(provinceSelected);//选择第一个input值
					if(areaSelected==provinceSelected) mythis.val(provinceSelected);
					$.ajax({
						type: "GET",
						url: "/common/area/" + provinceSelected,
						dataType: "json",
						success: function (data2) {
							var text='';
							$.each(data,function(key,val){
								if(val.value==provinceSelected){
									text=val.text;
								}
							});
							if(setvar.areaNull==true){
								json=[{"value":'',"text":text+"-不限"}]
								json=json.concat(data2);
							}else{
								json=data2;
							}
							oarea.resetOptions(json);
							if(areaSelected!=provinceSelected){
								oarea.resetSelected(areaSelected);//选择第二个input值
								if(areaSelected==citySelected)  mythis.val(areaSelected);
								$.ajax({
									type: "GET",
									url: "/common/city/" + areaSelected,
									dataType: "json",
									success: function (data3) {
										var text='';
										$.each(data2,function(key,val){
											if(val.value==areaSelected){
												text=val.text;
											}
										});
										if(setvar.cityNull==true){
											json=[{"value":'',"text":text+"-不限"}]
											json=json.concat(data3);
										}else{
											json=data3;
										}
										ocity.resetOptions(json);
										ocity.resetSelected(citySelected);//选择第三个input值
										mythis.val(citySelected);
										setvar.callback(citySelected,selectObjects.find("a").html());//回调
									}
								});
							}
						}
					});
				}
			}
		}
	});
})(jQuery);
/**
 * 使用说明:
 $("#address").selectArea({
		'province':$('#province'),
		'area':$('#area'),
		'city':$('#city'),
		//'provinceNull':true,//是否允许为空
		//'areaNull':true,//是否允许为空
		//'cityNull':true//是否允许为空
		'Selected':"331022"//默认选择值，建议给input也一个默认值
	});
 **/